package ydzhao.weixin.tuisong.job;

import ydzhao.weixin.tuisong.common.UserCom;
import ydzhao.weixin.tuisong.util.PushUpdate;

/**
 * @author chlong
 * @date 2022/8/10
 */

public class ThreadWorker {

    Object lock = new Object();

    /**
     * 获取消费者信息
     */
    public void getThread(String openId){
        if (openId.equals(UserCom.OPENID_YOU)){
            ThreadWorkerA threadWorkerA = new ThreadWorkerA();
            Thread thread = new Thread(threadWorkerA);
            thread.start();
        }else {
            ThreadWorkerB threadWorkerB = new ThreadWorkerB();
            Thread thread = new Thread(threadWorkerB);
            thread.start();
        }
    }


    class ThreadWorkerA implements Runnable{
        /**
         * When an object implementing interface <code>Runnable</code> is used
         * to create a thread, starting the thread causes the object's
         * <code>run</code> method to be called in that separately executing
         * thread.
         * <p>
         * The general contract of the method <code>run</code> is that it may
         * take any action whatsoever.
         *
         * @see java.lang.Thread#run()
         */
        @Override
        public void run() {
            synchronized (lock) {
                System.out.println("================");
                PushUpdate.pushUpdate(UserCom.OPENID_YOU);
                try {
                    // 预计2分钟推送答案
                    java.lang.Thread.sleep(1000*60*2);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                PushUpdate.pushAnswerUpdate(UserCom.OPENID_YOU);
                System.out.println("----------------------");
            }
        }
    }

    class ThreadWorkerB implements Runnable{
        /**
         * When an object implementing interface <code>Runnable</code> is used
         * to create a thread, starting the thread causes the object's
         * <code>run</code> method to be called in that separately executing
         * thread.
         * <p>
         * The general contract of the method <code>run</code> is that it may
         * take any action whatsoever.
         *
         * @see java.lang.Thread#run()
         */
        @Override
        public void run() {
            synchronized (lock) {
                System.out.println("================");
                PushUpdate.pushUpdate(UserCom.OPENID_ME);
                try {
                    // 预计2分钟推送答案
                    java.lang.Thread.sleep(1000*60*2);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                PushUpdate.pushAnswerUpdate(UserCom.OPENID_ME);
                System.out.println("----------------------");
            }
        }
    }
}
